﻿using LuaFramework;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

public class PackageMenu
{
    [MenuItem("Package/Build all to Resources")]
    public static void BuildToResources()
    {
        ToLuaMenu.BuildLuaToResources();

        var path = Application.dataPath + @"\Resources\Performance";

        if (Directory.Exists(path))
        {
            Directory.Delete(path, true);
        }

        PersistentDataMgr.CopyFiles(Application.dataPath + @"\Code\Performance\UI", path + @"\UI");
    }

    [MenuItem("Package/Build lua to AssetBundle (PC)")]
    public static void BuildLuaAssetBundlePC()
    {
        BuildLuaAssetBundle(BuildTarget.StandaloneWindows);
    }
    [MenuItem("Package/Build lua to AssetBundle (Android)")]
    public static void BuildLuaAssetBundleAndroid()
    {
        BuildLuaAssetBundle(BuildTarget.Android);
    }
    public static void BuildLuaAssetBundle(BuildTarget plantform)
    {
        AssetBundleBuild build = new AssetBundleBuild();
        build.assetBundleName = "lua." + PersistentDataMgr.assetBundleExtName;
        build.assetNames = new string[] {"Assets/Code/Performance/Lua/Require/require.lua.bytes"};
        //build.assetNames = new string[] { "Assets/Code/Performance/UI/Main/Login.prefab" };

        var abPath = Application.dataPath + "/" +AppConst.AssetDir + "/ABs";
        if (Directory.Exists(abPath))
        {
            Directory.Delete(abPath, true);
        }
        Directory.CreateDirectory(abPath);

        BuildPipeline.BuildAssetBundles("Assets/" + AppConst.AssetDir + "/ABs", new AssetBundleBuild[] { build }, BuildAssetBundleOptions.None, plantform);
    }


    private static List<AssetBundleBuild> allAssetBundle = new List<AssetBundleBuild>();
    [MenuItem("Package/Add Select AssetBundle 1")]
    public static void BuildSelectAssetBundle1()
    {
        AddAssetBundle("test1");
    }
    [MenuItem("Package/Add Select AssetBundle 2 ")]
    public static void BuildSelectAssetBundle2()
    {
        AddAssetBundle("test2");
    }
    private static void AddAssetBundle(string assetname)
    {
        var selectPathList = new List<string>();
        foreach (var guid in Selection.assetGUIDs)
        {
            selectPathList.Add(AssetDatabase.GUIDToAssetPath(guid));
        }

        var build = new AssetBundleBuild();
        build.assetBundleName = assetname;
        build.assetNames = selectPathList.ToArray();
        allAssetBundle.Add(build);
    }

    [MenuItem("Package/ClearAB")]
    public static void ClearAB()
    {
        allAssetBundle.Clear();

        TestReadAB.UnloadAB();
    }

    [MenuItem("Package/BuildAB")]
    public static void BuildAB()
    {
        var abPath = Application.dataPath + "/" + AppConst.AssetDir + "/testRelation";
        if (Directory.Exists(abPath))
        {
            Directory.Delete(abPath, true);
        }
        Directory.CreateDirectory(abPath);
        BuildPipeline.BuildAssetBundles("Assets/" + AppConst.AssetDir + "/testRelation", allAssetBundle.ToArray(), BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
    }

    [MenuItem("Package/ReadAB")]
    public static void ReadAB()
    {
        TestReadAB.ReadAB();
    }

}
